package com.hns.can.master.web;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.common.domain.GridPage;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.BaseParkingGpsInfoEntity;
import com.hns.can.master.entity.BaseParkingLotInfoEntity;
import com.hns.can.master.entity.CommCarInoutParkingInfoEntity;
import com.hns.can.master.service.SysParkingService;
import com.hns.can.master.vo.ParkingLotForCarParamVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.pub.DateTool;

/**
 * 停车场Controller
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/sysParking")
public class SysParkingController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(SysParkingController.class);
	
	//用于导出停车场信息
	final private String[] headerNames1 = new String[]{"编号","名称","所属机构","面积","容量","是否始发站"};
	final private String[] headerFields1 = new String[]{"parkingLotCd","parkingLotName","organName","parkingLotArea","parkingLotCapty","wthrStart"};
	
	//用于导出停车场内车辆进出信息
	final private String[] headerNames2 = new String[]{"停车场名称","运营线路","车牌号","出入场","出入场时间"};
	final private String[] headerFields2 = new String[]{"parkingLotName", "lineName", "licPltNo", "inoutType", "inoutTime"};

	
	@Autowired
	private SysParkingService sysParkingService;
	
	/**
	 * 新增停车场
	 * @param parkingInfo
	 * @return
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
    @RequestMapping("/addBaseParkingInfo")
    @ResponseBody
	public Object addBaseParkingInfo(BaseParkingLotInfoEntity parkingInfo)  throws Exception {    	
    	return sysParkingService.addParkingInfo(parkingInfo, this.getCurrentUser().getUser());
	}
    
    /**
     * 更新停车场
     * @param parkingInfo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/updBaseParkingInfo")
    @ResponseBody
	public Object updBaseParkingInfo(BaseParkingLotInfoEntity parkingInfo)  throws Exception {    	
    	sysParkingService.updParkingInfo(parkingInfo, this.getCurrentUser().getUser());
		return getReturnMsg("更新停车场成功");
	}
    
    /**
     * 删除停车场
     * @param parkingId
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/delBaseParkingInfo")
    @ResponseBody
    public Object delBaseParkingInfo(String parkingId) throws Exception {
    	sysParkingService.delParkingInfo(parkingId);
    	return getReturnMsg("删除停车场成功");
    }
    
    /**
     * 根据ID获取停车场基本信息
     * @param parkingId
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getBaseParkingLotInfoById")
    @ResponseBody
    public Object getBaseParkingLotInfo(String parkingId){
    	BaseParkingLotInfoEntity entity=sysParkingService.getBaseParkingLotInfoById(parkingId);
    	return this.getReturnResult(entity);
    }
    
    /**
     * 根据ID获取停车场经纬度集
     * @param parkingId
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getBaseParkingGpsListById")
    @ResponseBody
    public Object getBaseParkingGpsListById(String parkingId){
    	List<BaseParkingGpsInfoEntity> list=sysParkingService.getBaseParkingGpsListById(parkingId);
    	return this.getReturnResult(list);
    }
    
    /**
     * 分页查询停车场基本信息
     * @param organId
     * @param parkingLotName
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchParkingInfoPage")
    @ResponseBody
    public Object searchParkingInfoPage(String organId,String parkingLotName) throws Exception{
    	Map<String, Object> paramMap=new HashMap<String, Object>();
    	String corpId=this.getCurrentUser().getUser().getCorpId();
    	paramMap.put("corpId", corpId);
    	paramMap.put("organId", organId);
    	paramMap.put("parkingLotName", parkingLotName);    	
    	Pagination page = sysParkingService.searchParkingInfoPage(paramMap);
    	GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 根据用户OrgId获取停车场
     * @return
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/getParkingInfoListByOrgId")
    @ResponseBody
    public Object getParkingInfoListByOrgId(){
    	String orgId=this.getCurrentUser().getUser().getOrganId();
    	List<BaseParkingLotInfoEntity> list =sysParkingService.getParkingInfoListByOrgId(orgId);
    	return this.getReturnResult(list);
    }
    
    /**
     * 分页查询停车场进场车辆
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchCarInoutParkingInfoPage")
    @ResponseBody
    public Object searchCarInoutParkingInfoPage(ParkingLotForCarParamVo vo) throws Exception{
    	Pagination page = sysParkingService.searchCarInoutParkingInfoPage(vo,this.getCurrentUser().getUser());
    	GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 分页查询停车场内车辆汇总
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchParkingLotCarTotalPage")
    @ResponseBody
    public Object searchParkingLotCarTotalPage(ParkingLotForCarParamVo vo) throws Exception{
    	Pagination page = sysParkingService.searchParkingLotCarTotalPage(vo,this.getCurrentUser().getUser());
    	GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 分页查询停车场内车辆明细
     * @param vo
     * @return
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/searchParkingLotCarDetailPage")
    @ResponseBody
    public Object searchParkingLotCarDetailPage(ParkingLotForCarParamVo vo) throws Exception{
    	Pagination page = sysParkingService.searchParkingLotCarDetailPage(vo,this.getCurrentUser().getUser());
    	GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 导出停车场车辆进出场信息Excel
     * @param vo
     * @param response
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportCarInoutParkingInfoExcel")
    public void exportCarInoutParkingInfoExcel(ParkingLotForCarParamVo vo,HttpServletResponse response) throws Exception{
    	List<CommCarInoutParkingInfoEntity> list= sysParkingService.exportCarInoutParkingInfo(vo,this.getCurrentUser().getUser());
    	ExportUtil.exportFiles(Arrays.asList(headerNames2), Arrays.asList(headerFields2), list, response, "excel", URLEncoder.encode("停车场进出明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "停车场出入车辆数据");
    }
    
    /**
     * 导出停车场车辆进出场信息Pdf
     * @param vo
     * @param response
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportCarInoutParkingInfoPdf")
    public void exportCarInoutParkingInfoPdf(ParkingLotForCarParamVo vo,HttpServletResponse response) throws Exception{
    	List<CommCarInoutParkingInfoEntity> list= sysParkingService.exportCarInoutParkingInfo(vo,this.getCurrentUser().getUser());
		ExportUtil.exportFiles(Arrays.asList(headerNames2), Arrays.asList(headerFields2), list, response, "pdf", URLEncoder.encode("停车场进出明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "停车场出入车辆数据");
    }
    
    /**
     * 导出停车场信息Excel
     * @param organId
     * @param parkingLotName
     * @param response
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportParkingInfoExcel")
    public void exportParkingInfoExcel(String organId,String parkingLotName,HttpServletResponse response) throws Exception{
    	List<BaseParkingLotInfoEntity> list= sysParkingService.exportParkingInfo(organId,parkingLotName);
    	ExportUtil.exportFiles(Arrays.asList(headerNames1), Arrays.asList(headerFields1), list, response, "excel", "ParkingInfo"+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "停车场数据");
    }
    
    /**
     * 导出停车场信息Pdf
     * @param organId
     * @param parkingLotName
     * @param response
     * @throws Exception
     * @author:zhangzq
     * @email:
     */
    @RequestMapping("/exportParkingInfoPdf")
    public void exportParkingInfoPdf(String organId,String parkingLotName,HttpServletResponse response) throws Exception{
    	List<BaseParkingLotInfoEntity> list= sysParkingService.exportParkingInfo(organId,parkingLotName);
    	ExportUtil.exportFiles(Arrays.asList(headerNames1), Arrays.asList(headerFields1), list, response, "pdf", "ParkingInfo"+DateTool.getCurrentSysDate("yyyyMMddHHmmss"), "停车场数据");
    }
    
}
